/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:assign-tracer
 * Create Time: 2016年2月24日 下午10:58:06
 */
package com.lenovo.lmrp.server.assigntracer.domain.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lenovo.lmrp.api.constant.TicketPolicy;
import com.lenovo.lmrp.api.model.entity.AssigningTicketPO;
import com.lenovo.lmrp.api.model.entity.EngineerPO;
import com.lenovo.lmrp.api.model.entity.TicketExtensePO;
import com.lenovo.lmrp.api.model.entity.TicketPO;
import com.lenovo.sdc.framework.dao.ADaoFactory;
import com.lenovo.sdc.framework.dao.ICommonDAO;
import com.lenovo.sdc.framework.util.EncryptUtil;
import com.lenovo.sdc.framework.util.exception.ErrorCode;
import com.lenovo.sdc.framework.util.exception.TraceableException;

/**
 * 
 * @author songyz
 * @version v1.0
 */
public class TicketService {
	
	private final static Logger logger = LoggerFactory.getLogger(TicketService.class);
	/**
	 * 将派单的工单记录存入数据库（TicketAssignPolicy表）中
	 * 
	 * @param itsmCode
	 * @param engineerPOList
	 * @param ticketPO
	 */
	public void storeAssigningTicketPO(String itsmCode, List<EngineerPO> engineerPOList, TicketPO ticketPO) {
		AssigningTicketPO assigningTicketPO = null;
		// 将派单的工单记录存入数据库（TicketAssignPolicy表）中
		
		List<String> list = new ArrayList<>();
		engineerPOList.forEach(po ->{
			list.add(po.getId());
		});
		
		logger.info("ticketid: {},engineers: {}", ticketPO.getId(), list);

		for (EngineerPO engineerPO : engineerPOList) {
			assigningTicketPO = new AssigningTicketPO();
			assigningTicketPO.setAssignPolicy(ticketPO.getPolicy());
			assigningTicketPO.setEngineerId(engineerPO.getId());
			assigningTicketPO.setId(EncryptUtil.getUUID());
			assigningTicketPO.setNotifyTime(new Date());
			assigningTicketPO.setTicketId(ticketPO.getId());

			try (ICommonDAO dao = ADaoFactory.getDefault().createDAO(itsmCode)) {
				dao.beginTrans();
				dao.save(assigningTicketPO);
				dao.endTrans();
			} catch (Exception e) {
				throw new TraceableException(ErrorCode.DB_INSERT_FAILED, e);
			}
		}
	}

	/**
	 * 抢单完成操作,更新工单受理人
	 * 
	 * @param itsmCode
	 * @param engineerID
	 * @param po
	 */
	public void completeGrab(String itsmCode, String engineerID, TicketPO po) {
		try (ICommonDAO dao = ADaoFactory.getDefault().createDAO(itsmCode)) {
			dao.beginTrans();
//			List<AssigningTicketPO> assignRecords = dao.filteByCondition(AssigningTicketPO.class,
//					new RecordFilter().addCondition(new Condition<String, Object>("ticketid", po.getId())).addCondition(
//							new Condition<String, Object>("assignpolicy", TicketPolicy.GRAB.getPolicy()),
//							RelationEnum.AND));
//			for (AssigningTicketPO assigningTicketPO : assignRecords)
//				// 删除抢单推送记录
//				dao.delete(assigningTicketPO);
			//删除抢单推送的中间表
			String hql = "delete from AssigningTicketPO where ticketid = ? and assignpolicy = ?";
			dao.executeHQL(hql, new Object[] { po.getId(), TicketPolicy.GRAB.getPolicy() });

			// 更新工单的受理人和工单的状态
			dao.update(po);
			dao.endTrans();
		} catch (Exception e) {
			throw new TraceableException(ErrorCode.DB_UPDATE_FAILED, e);
		}
	}

	/**
	 * 封装工单的扩展信息
	 * 
	 * @param itsmCode
	 * @param ticketPO
	 * @return
	 */
	public TicketPO setTicketPoExtense(String itsmCode, TicketPO ticketPO) {
		try (ICommonDAO dao = ADaoFactory.getDefault().createDAO(itsmCode)) {
			ticketPO.setOthers(dao.get(TicketExtensePO.class, ticketPO.getId()));
			return ticketPO;
		} catch (Exception e) {
			throw new TraceableException(ErrorCode.DB_QUERY_FAILED, e);
		}
	}
}
